﻿#if NETFX
using Color = System.Windows.Media.Color;
#elif NETFX_CORE || WINDOWS_PHONE
using Color = Windows.UI.Color;
#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace NicoView.Model.Service
{
    /// <summary>
    /// 名前の付いた色情報
    /// </summary>
    public sealed class NamedColor:IComparable<NamedColor>,IEquatable<NamedColor>
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="color"></param>
        /// <param name="name"></param>
        public NamedColor(Color color, string name)
        {
            Name = name;
            Color = color;
        }


        /// <summary>
        /// 色の名前
        /// </summary>
        public string Name { get;  set; }

        /// <summary>
        /// 関連づいた色
        /// </summary>
        public Color Color { get;  set; }


        /// <summary>
        /// 色情報を数値に変換
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        private static int _colorAsInt(Color color)
        {
            return (color.A * 256 * 256 * 256) + (color.R * 256 * 256) + (color.G * 256) + color.B;
        }

        /// <summary>
        /// 共通で使われるNULLカラー
        /// </summary>
        public static readonly NamedColor NoneColor = new NamedColor(new Color(), "なし");


        /// <summary>
        /// 比較の際に使用されるメソッド
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(NamedColor other)
        {
            int baseColor = this.GetHashCode();
            int targetColor = other.GetHashCode();
            return baseColor - targetColor;
        }


        /// <summary>
        /// オーバーライドされたGetHashCode
        /// 比較の際に使用される
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return _colorAsInt(this.Color);
        }

        /// <summary>
        /// 渡されたオブジェクトと等しいか判定
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            var rslt = GetHashCode() - obj.GetHashCode();
            return rslt == 0;
        }

        /// <summary>
        /// 渡されたNamedCorlorと等しいか判定
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(NamedColor other)
        {
            if (other == (object)null) return false;
            return this.Color == other.Color;
        }
        
        /// <summary>
        /// ==演算子のオーバーロード
        /// </summary>
        /// <param name="target1"></param>
        /// <param name="target2"></param>
        /// <returns></returns>
        public static bool operator ==(NamedColor target1, NamedColor target2)
        {
            if (target1 == (object)null || target2 == (object)null)
            {
                return ((object)target1) == ((object)target2);
            }
            var rslt = target1.Equals(target2);
            return rslt;
        }

        /// <summary>
        /// !=演算子のオーバーロード
        /// </summary>
        /// <param name="target1"></param>
        /// <param name="target2"></param>
        /// <returns></returns>
        public static bool operator !=(NamedColor target1, NamedColor target2)
        {
            return !(target1 == target2);
        }


        
        
    }
}
